package com.yxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxr.constants.SystemCanstants;
import com.yxr.domain.ResponseResult;
import com.yxr.domain.entity.SgArticle;
import com.yxr.domain.entity.SgCategory;
import com.yxr.domain.vo.ArticleDetailVo;
import com.yxr.domain.vo.ArticleListVo;
import com.yxr.domain.vo.HotArticleVo;
import com.yxr.domain.vo.PageVo;
import com.yxr.mapper.SgArticleMapper;
import com.yxr.service.SgArticleService;
import com.yxr.service.SgCategoryService;
import com.yxr.utils.BeanCopyUtils;
import com.yxr.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;



@Service
public class SgArticleServiceImpl extends ServiceImpl<SgArticleMapper, SgArticle> implements SgArticleService  {


    @Autowired
   private SgCategoryService categoryService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private SgArticleService articleService;


    @Override
    public ResponseResult hotArticleList() {
        //从redis查询viewcount并且更新到数据库
        Map<String, Integer> cacheMap = redisCache.getCacheMap(SystemCanstants.REDIS_VIEWCOUNT_PREFIX);
        List<SgArticle> collect = cacheMap.entrySet().stream().map(article -> new SgArticle(Long.valueOf(article.getKey()), article.getValue()))
                .collect(Collectors.toList());
        articleService.updateBatchById(collect);


        //查询热门文章
        LambdaQueryWrapper<SgArticle> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(SgArticle::getStatus, SystemCanstants.ARTICLE_STATUS_NORMAL);
        //浏览量排序
        queryWrapper.orderByDesc(SgArticle::getViewCount);
        //最多查询十条
        Page<SgArticle> page = new Page<>(1,10);
        page(page,queryWrapper);

        List<SgArticle> articles = page.getRecords();

        //赋值优化
        //List<HotArticleVo> hotArticleVos =new ArrayList<>();

//        for (SgArticle article : articles) {
//            HotArticleVo vo = new HotArticleVo();
//            BeanUtils.copyProperties(article,vo);
//            hotArticleVos.add(vo);
//        }
        List<HotArticleVo> hotArticleVos1 = BeanCopyUtils.copyBeanaList(articles, HotArticleVo.class);


        return ResponseResult.okResult(hotArticleVos1);
    }
   @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
       //从redis查询viewcount并且更新到数据库
       Map<String, Integer> cacheMap = redisCache.getCacheMap(SystemCanstants.REDIS_VIEWCOUNT_PREFIX);
       List<SgArticle> collect = cacheMap.entrySet().stream().map(article -> new SgArticle(Long.valueOf(article.getKey()), article.getValue()))
               .collect(Collectors.toList());
       articleService.updateBatchById(collect);

        //查询条件
       LambdaQueryWrapper<SgArticle> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        //如果有categoryId 就要求查询和传入的相同
       lambdaQueryWrapper.eq(categoryId != null&&categoryId>0,SgArticle::getCategoryId,categoryId);
        // 状态是正式发布的文章
       lambdaQueryWrapper.eq(SgArticle::getStatus,SystemCanstants.ARTICLE_STATUS_NORMAL);
        //对isTop降序
       lambdaQueryWrapper.orderByDesc(SgArticle::getIsTop);
        //分页查询
        Page<SgArticle> page = new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);

       //查询分类名称
        List<SgArticle> articles= page.getRecords();
        articles.stream()
              .map(new Function<SgArticle, SgArticle>() {
                    @Override
                    public SgArticle apply(SgArticle sgArticle) {
                        //获取分类名称
                        SgCategory category = categoryService.getById(sgArticle.getCategoryId());
                        String name = category.getName();
                        sgArticle.setCategoryName(name);
                        //设置名称
                        return sgArticle;
                    }
                }).collect(Collectors.toList());


        //封装结果
       List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanaList(page.getRecords(), ArticleListVo.class);
       PageVo pageVo = new PageVo(articleListVos,page.getTotal());
     return ResponseResult.okResult(pageVo);
   }

    @Override
    public ResponseResult getArticalDetail(Long id) {
        SgArticle artical = getById(id);
        //从redis获取viewcount
        Integer viewcount = redisCache.getCacheMapValue(SystemCanstants.REDIS_VIEWCOUNT_PREFIX, id.toString());
        artical.setViewCount(viewcount.longValue());
        ArticleDetailVo articlDetail = BeanCopyUtils.copyBean(artical, ArticleDetailVo.class);
        Long categoryId = articlDetail.getCategoryId();
        SgCategory category = categoryService.getById(categoryId);

        if (!Objects.isNull(category)){
            articlDetail.setCategoryName(category.getName());
        }

        return ResponseResult.okResult(articlDetail);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        //更新redis中对应id的浏览量
        redisCache.incrementCacheMapValue(SystemCanstants.REDIS_VIEWCOUNT_PREFIX,id.toString(),1);
        return ResponseResult.okResult();
    }
}
